package com.castle.fortress.admin.goods.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsOptionDto;
import com.castle.fortress.admin.goods.entity.GoodsOptionEntity;
import com.castle.fortress.admin.goods.service.GoodsOptionService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格项目详情 api 控制器
 *
 * @author majunjie
 * @since 2021-11-25
 */
@Api(tags="商品规格项目详情api管理控制器")
@RestController
@RequestMapping("/api/goods/goodsOption")
public class ApiGoodsOptionController {
    @Autowired
    private GoodsOptionService goodsOptionService;


    /**
     * 商品规格项目详情的分页展示
     * @param goodsOptionDto 商品规格项目详情实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("商品规格项目详情分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsOptionDto>> pageGoodsOption(GoodsOptionDto goodsOptionDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsOptionDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsOptionDto> pages = goodsOptionService.pageGoodsOption(page, goodsOptionDto);
        return RespBody.data(pages);
    }

    /**
     * 商品规格项目详情保存
     * @param goodsOptionDto 商品规格项目详情实体类
     * @return
     */
    @ApiOperation("商品规格项目详情保存")
    @PostMapping("/save")
    public RespBody<String> saveGoodsOption(@RequestBody GoodsOptionDto goodsOptionDto){
        if(goodsOptionDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOptionEntity goodsOptionEntity = ConvertUtil.transformObj(goodsOptionDto,GoodsOptionEntity.class);
        if(goodsOptionService.save(goodsOptionEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格项目详情编辑
     * @param goodsOptionDto 商品规格项目详情实体类
     * @return
     */
    @ApiOperation("商品规格项目详情编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoodsOption(@RequestBody GoodsOptionDto goodsOptionDto){
        if(goodsOptionDto == null || goodsOptionDto.getId() == null || goodsOptionDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOptionEntity goodsOptionEntity = ConvertUtil.transformObj(goodsOptionDto,GoodsOptionEntity.class);
        if(goodsOptionService.updateById(goodsOptionEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格项目详情删除
     * @param ids 商品规格项目详情id集合
     * @return
     */
    @ApiOperation("商品规格项目详情删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoodsOption(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsOptionService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格项目详情详情
     * @param id 商品规格项目详情id
     * @return
     */
    @ApiOperation("商品规格项目详情详情")
    @GetMapping("/info")
    public RespBody<GoodsOptionDto> infoGoodsOption(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsOptionEntity goodsOptionEntity = goodsOptionService.getById(id);
            GoodsOptionDto goodsOptionDto = ConvertUtil.transformObj(goodsOptionEntity,GoodsOptionDto.class);
        return RespBody.data(goodsOptionDto);
    }


}
